package com.log.blog_backend.service.impls;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.log.blog_backend.Dao.ArticleDetail;
import com.log.blog_backend.common.Result;
import com.log.blog_backend.entity.Article;
import com.log.blog_backend.mapper.ArticleMapper;
import com.log.blog_backend.service.ArticleService;
import com.log.blog_backend.utils.CutStringByComma;
import com.log.blog_backend.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Autowired
    TokenUtil tokenUtil;

    /**
     * 保存草稿
     * @param article
     * @param httpServletRequest
     * @return Result.ok()
     */
    @Override
    public Result savePage(Article article, HttpServletRequest httpServletRequest) {
        if(articleMapper.selectById(article)==null){
            //获取UserId
            String userId = tokenUtil.getUserIdByToken(httpServletRequest);
            //加工 article
            article.setUserId(userId);
            article.setIsrelease(1);
            //插入
            articleMapper.insert(article);

        }else{
            articleMapper.updateById(article);
        }
        return Result.ok();


    }

    /**
     * 发布文章
     * @param article
     * @param httpServletRequest
     * @return
     */
    @Override
    public Result uploadArticle(Article article, HttpServletRequest httpServletRequest) {
        if(articleMapper.selectById(article)==null){
            //获取UserId
            String userId = tokenUtil.getUserIdByToken(httpServletRequest);
            //加工 article
            article.setUserId(userId);
            article.setIsrelease(0);
            //插入
            articleMapper.insert(article);

        }else{
            article.setIsrelease(0);
            articleMapper.updateById(article);
        }
        return Result.ok();
    }

    @Override
    public Article getArticle(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        String tagList = articleMapper.getTagList(articleId);
        CutStringByComma cutStringByComma=new CutStringByComma();
        List<String> cut = cutStringByComma.cut(tagList);
        article.setTags(cut);
        return article;
    }

    @Override
    public Result getHotpotArticles() {
        QueryWrapper<Article>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id","365656205332451328");
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.ok(articles);
    }

    @Override
    public Result getArticleListBySearchContent(String searchContent) {
        if (StringUtils.isNotBlank(searchContent)){
            List<ArticleDetail> articleDetail = articleMapper.getArticleDetail(searchContent);
            return Result.ok(articleDetail);
        }

        return null;
    }

    @Override
    public Result countUserReleaseArticle(Long userId) {
        QueryWrapper<Article>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("isrelease",0);
        Integer count = articleMapper.selectCount(queryWrapper);
        return Result.ok(count);
    }

    /**
     * 根据userId获取所有的文章
     * @param httpServletRequest
     * @return
     */
    @Override
    public Result getAllArticles(HttpServletRequest httpServletRequest) {
        //获取UserId
        String userId = tokenUtil.getUserIdByToken(httpServletRequest);
        log.info("userid"+userId);
        //
        QueryWrapper<Article>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.ok(articles);
    }


}
